home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / contrib / partingr / addscore.nw (.txt) < prev    next >
LaTeX Document  |  1995-02-24  |  6KB  |  152 lines

  1. \def\musecs{$\mu$secs}
  2. \itemwidth=.25in
  3. \section{Archery Database: {\tt AddScore}}
  4. This perl script adds a score (or scores) to the archery database.
  5. It provides only a basic user interface.
  6. <<addscore>>=
  7. <<setup>>
  8. <<main program loop>>
  9. <<subroutines>>
  10. \section{Setting up the database and script}
  11. Because much of the code is shared between this family of scripts,
  12. some of the code is in subroutine files. We [[require]] these.
  13. <<setup>>=
  14. require "custom" || die "can't open custom routines library";
  15. require "archsubs" || die "can't open subroutines library";
  16. @ %def &readrounds &init
  17. Early versions of this script allowed you to abort an entry by not
  18. typing anything at a prompt. However this became cumbersome when only
  19. the third prompt would let you do this. So we now trap [[SIGINT]]
  20. and point it to the same abort routine.
  21. <<setup>>=
  22. $SIG{'INT'}='abort';
  23. @ %def $SIG
  24. We need know what date it is today (for the default database and
  25. date) so we get this from the operating system via the [[gmtime()]]
  26. call.
  27. <<setup>>=
  28. $start=time;
  29. ($ts,$tmi,$th,$tmd,$tmo,$ty,@junk)=gmtime($start);
  30. @ %def $start $ts $tmi $th $tmd $tmo $ty @junk | $start gmtime()
  31. Now we read in the rounds database, and open the relevant dbm file for
  32. the archery database. [[&init]] handles the command line options that
  33. relate to the person and year that are asked for. 
  34. <<setup>>=
  35. &readrounds;
  36. do &init;
  37. @ %def | &readrounds &init
  38. \section{The main body of the program}
  39. The main part of the program justs loops asking the user for
  40. a score to be entered and then prompting for another loop.
  41. Unfortunately, we still treat dates as strings (due to problems in the
  42. conversion of dates to \musecs\ and back) so all this data cannot
  43. be accessed by date order.
  44. \subsection{Outline of main loop}
  45. <<main...>>=
  46. while(1)
  47.   <<date entry and validation>>
  48.   <<round entry and validation>>
  49.   <<score entry and validation>>
  50.   <<create and store dbm entry>>
  51.   <<prompt user for another go>>
  52. We close the dbm database explicitly here, just to be on the safe side.
  53. <<main...>>=
  54. dbmclose(scores);
  55. \subsection{Entering the date}
  56. <<date...>>=
  57.   dateloop:
  58.   while(1)
  59.       print "Enter date ($tmd-$tmo-$year) - ";
  60.       $date=scalar(<STDIN>);
  61.       chop $date;
  62.       $date=join('-',$tmd,$tmo) unless $date;
  63.       if($date=~/(\d{1,2})-(\d{1,2})/)
  64.       {
  65.           $dt_ntrd=1;
  66.           $dindex=sprintf("%02d:%02d:%04d",$1,$2,$year);
  67.           $usecs=&retime($1,$2,$year);
  68.           ($xts,$xtmi,$xth,$xtmd,$xtmo,$xty,@junk)=gmtime($usecs);
  69.           printf "%02d-%02d-%04d\n",$xtmd,$xtmo,$xty;
  70.           last dateloop;
  71.       }
  72.       else { print "invalid date\n"; }
  73.   }    
  74. @ %def dateloop: $date $dt_ntrd $dindex $usecs $xts $xtmi $xth $xtmd $xtmo $xty @junk | STDIN &retime() gmtime()
  75. \subsection{Entering the round}
  76. <<round...>>=
  77.   $round=&getround;
  78.   ($rtype,@rdists)=split(/,/,@rounds{$round});
  79.   $mult=($rtype='y'?9:10);
  80. @ %def $round @rdists $mult | $rtype @rounds{} &getround
  81. \subsection{Entering the score}
  82. <<score...>>=
  83.   until(defined($sc_ntrd))
  84.       print 'Enter scores (100,100,100) - ';
  85.       $sclist=scalar(<STDIN>);
  86.       chop $sclist;
  87.       &abort unless $sclist;
  88.       @rscores=split(/,/,$sclist);
  89.       if(2*$#rscores==$#rdists-1) {$sc_ntrd=1;}
  90.       else { print "wrong number of scores\n"; }
  91.       for($i=0;$i<=$#rscores;$i++)
  92.       {
  93.           $ms=@rdists[2*$i]*$mult;
  94.           if(@rscores[$i]>$ms)
  95.           { printf "invalid: %d>possible ($ms)\n",@rscores[$i];
  96.             $sc_ntrd=$undefined;
  97.           }
  98.       }
  99. @ %def $sclist @rscores $ms $sc_ntrd | STDIN $mult @rdists $undefined
  100. \subsection{Storing the data}
  101. The dbm entries are created in a fairly naive fashion at the moment.
  102. The round name and scores are turned into a CSV string. Future versions
  103. will almost certainly use templates and packing to reduce the amount
  104. of information that is stored.
  105. <<create...>>=
  106.   $entry=join(',',$round,@rscores);
  107.   @scores{$dindex}=$entry;
  108. @ %def $entry @scores | $round @rscores $dindex
  109. \subsection{Prompt the user}
  110. Now we prompt the user to see if they want to enter another score.
  111. The default answer (ie just pressing [[RETURN]]) is yes on the
  112. assumption that more than one score will be entered at a time. It
  113. will be changed so that a score entered with today's date will
  114. change the default to no on the assumption that the rest of the
  115. database is up to date.
  116. <<prompt...>>=
  117.   $loopagain=&yesno("Another score",'y');
  118.   last unless $loopagain;
  119.   undef $sc_ntrd;
  120. @ %def $loopagain | &yesno() $sc_ntrd
  121. \section{Subroutines}
  122. \subsection{[[retime]]}
  123. This subroutine is supposed to convert a date in dd-mm-yyyy form into
  124. the number of \musecs\ since 1-1-1970, but it sometimes gets it
  125. mysteriously wrong, so we don't use it for indexing yet.
  126. <<Subroutines>>=
  127. sub retime
  128. { local($d,$m,$y)=@_;
  129.   local($wm,$md,$yd);
  130.   $wm=$m-3;
  131.   if($wm<0) { $wm+=12; $y--; }        # modified month number
  132.   $md=int(30.6*$wm+.5);            # month day
  133.   $yd=int(365.25*($y-1970)+$md+$d)+60;    # actual day number
  134.   86400*$yd;
  135. @ %def $d $m $y $wm $md $yd
  136. \section{Things to do}
  137. \itemize
  138. \item
  139. The way the rounds are stored and accessed needs to be changed.
  140. `Compiling' the rounds into a more compact form would save space,
  141. and possibly make things a little faster.
  142. \item
  143. A better user interface would also be a good idea. Possibly making
  144. the program run under a different screen mode for larger text.
  145. \item
  146. Adding a confirm just before the data is inserted into the dbm database.
  147. \enditemize
  148. \chapter{Variables}
  149. \printindex{ids}
  150. \chapter{Modules}
  151. \printindex{mods}
  152.